#include "entry/logic/EntryLogic.h"

DoorLogic::DoorLogic(QMainWindow *parent):DoorWin(parent){
    HintPlEdit->setPlainText(RUNNINGENV.join("\n"));
    confile_opening();
};

void DoorLogic::Md5Error(QAbstractButton *Button){
    if (Button->text() == "be use for this time" ) {
        diary("use for this time of the rules file", Diary::Warning);
    } else if (Button->text() == "update md5 and use it" ) {
        RLUPDTINFP.insert("MD5", LCDBMAP["DBKV"].second);
        __CFG_UPDT__(RLUPDTINFP);
        diary("use the rules file for after update md5", Diary::Warning);
    } else if (QStringList({"reset now", "i got it"}).contains(Button->text())) {
        RLUPDTINFP.insert("MD5", LCDBMAP["DBKV"].second);
        __INIT__(true);
        diary("use the rules file for after initialize", Diary::Debug);
    }
}

void DoorLogic::confile_opening(){
    QColor relieveColor(241,242,245);
    QColor mutexColor(255,92,92);
    if (MapForStat[config_open]){
        config_open->doconfigur_text("打开配置");
        config_save->setEnabled(false);
        config_update->setEnabled(false);
        config_reset->setEnabled(false);
        MapForStat[config_open] = false;
        config_back[MSEVKEY.WIN_PRESS] = mutexColor;
        config_save->doconfigur_backcolorgroup(config_back);
        config_reset->doconfigur_backcolorgroup(config_back);
        config_update->doconfigur_backcolorgroup(config_back);
        config_save->doconfigur_lock();
        config_reset->doconfigur_lock();
        config_update->doconfigur_lock();
        MapForStat[config_open] = false;
        if (!RUNNINGENV.isEmpty()) HintPlEdit->setPlainText(RUNNINGENV.join("\n"));

    } else {
        config_open->doconfigur_text("退出配置");
        config_save->setEnabled(true);
        config_update->setEnabled(true);
        config_reset->setEnabled(true);
        config_back[MSEVKEY.WIN_PRESS] = relieveColor;
        config_save->doconfigur_backcolorgroup(config_back);
        config_update->doconfigur_backcolorgroup(config_back);
        config_reset->doconfigur_backcolorgroup(config_back);
        config_save->doconfigur_unlock();
        config_reset->doconfigur_unlock();
        config_update->doconfigur_unlock();
        MapForStat[config_open] = true;
        __CFG_READ__();
        if (!RGLRLSMAP["ALL"]["TEXT"].isEmpty()) HintPlEdit->setPlainText(RGLRLSMAP["ALL"]["TEXT"]);
    }
}

void DoorLogic::confile_save(){
    QString RglContent = HintPlEdit->toPlainText();
    __CFG_WRITE__(RLPATH, RglContent);
    __CFG_READ__();
    emit configAmend();
}

void DoorLogic::confile_amend(){
    __INIT__();
    if (!RGLRLSMAP["ALL"]["TEXT"].isEmpty()){
        HintPlEdit->setPlainText(RGLRLSMAP["ALL"]["TEXT"]);
    }
    emit configAmend();
}

void DoorLogic::confile_reset(){
    RLUPDTINFP.insert("MD5", LCDBMAP["DBKV"].second);
    __INIT__(true);
    if (!RGLRLSMAP["ALL"]["TEXT"].isEmpty()){
        HintPlEdit->setPlainText(RGLRLSMAP["ALL"]["TEXT"]);
    }
    emit configAmend();
}

/*#存在主窗口，子窗口已全部关闭时，主窗口点击关闭按钮则为调用close，子窗口未全部关闭时，点击关闭按钮则调用setVisible将主窗口隐藏;
主窗口隐藏，如果子窗口已全部关闭，则调用close
**修复项: 增加累计机制，子窗口全部累计关闭后才调用close，否则调用close后数据库，查询组件释放，子窗口触发查询时会直接造成闪退; 替代方案也可以在子窗口判断并调用数据库检查函数避免*/
void DoorLogic::mainloginc_quitrequest(bool stat, NT::WIN_QUIT_ATTRIBUTE Type){
    int value;
    stat ? value = -1 : value = 1;
    Rdomap_QuiCount[Type] += value;
    switch (Type)
    {
    case NT::MAIN_DOOR_QUIT:
        if (Rdomap_QuiCount[NT::CHILD_TOOL_QUIT] || Rdomap_QuiCount[NT::CHILD_MEDIA_QUIT])
        {
            setVisible(false);

        }
        else if (!Rdomap_QuiCount[NT::CHILD_TOOL_QUIT] && !Rdomap_QuiCount[NT::CHILD_MEDIA_QUIT])
        {
            close();
        }
        break;
    case NT::CHILD_TOOL_QUIT:
    case NT::CHILD_MEDIA_QUIT:
        if (!isVisible())
        {
            if (!Rdomap_QuiCount[NT::CHILD_TOOL_QUIT] && !Rdomap_QuiCount[NT::CHILD_MEDIA_QUIT])
            {
                close();
            }
        }
        break;
    }
}

void DoorLogic::mang_dependableload(){
    QRegularExpression dreg("\\d+");
    QString _width = RGLRLSMAP["MANG"]["WindownGeometryW"];
    QString _height = RGLRLSMAP["MANG"]["WindownGeometryH"];
    if (definfc.Max(dreg.match(_width).capturedTexts()).toInt() == _width.size() &&
        definfc.Max(dreg.match(_height).capturedTexts()).toInt() == _height.size())
    {
        int width = _width.toUInt();
        int height = _height.toUInt();
        (width && height) ? resize(width, height) : resize(580, 350);
        diary("entry win setVisible->true", Diary::Debug)
    }
    setVisible(true);
    d_margin_radius(QMargins(3, 0, 3, 3), 4);
    MessageBoxQ *Md5ErrorMsg;
    if (!MD5CHECK && CTCHECK ){
        Md5ErrorMsg = new MessageBoxQ(this,
                                      {"be use for this time", "update md5 and use it"},
                                      MessageBoxQ::MSG_Question,
                                      "question",
                                      "md5 compare error, select operations");
    } else if (MD5CHECK && !CTCHECK ){
        QString FFHintChar = "ini file content check is fail, loss key[ %1 ], be reset it?";
        Md5ErrorMsg = new MessageBoxQ(this,
                                      {"reset now", "ignore"},
                                      MessageBoxQ::MSG_Question,
                                      "question",
                                      FFHintChar.arg(_INI_LOSSKEY_.join(" ")));

    } else if (!MD5CHECK && !CTCHECK ){
        Md5ErrorMsg = new MessageBoxQ(this,
                                      {"i got it"},
                                      MessageBoxQ::MSG_Question,
                                      "question",
                                      "ini file content happend bad damege, is reset now");

    } else Md5ErrorMsg = nullptr;
    if (Md5ErrorMsg) connect(Md5ErrorMsg, &MessageBoxQ::checkEvent, this, &DoorLogic::Md5Error);

}
